package com.futao.support;

import face.com.futao.dao.IUserDao;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.realm.AuthenticatingRealm;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;


/**
 * @author futao
 * Created on 18-6-13-下午1:39.
 */
@Component
public class CustomRealm extends AuthenticatingRealm {

    @Resource
    private IUserDao iUserDao;

    /**
     * @param authenticationToken
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        //1.从主体传过来的认证信息中，获得用户名
        String userName = (String) authenticationToken.getPrincipal();
        //2.通过用户名获得密码
        String password = getPasswordByUserName(userName);
        if (password == null) {
            return null;
        }
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userName, password, "customRealm");
        return simpleAuthenticationInfo;

    }

    private String getPasswordByUserName(String userName) {
        return iUserDao.getUserByUserName(userName).get(0).getName();
    }


}
